﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Loaddata.getemisionvalue" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;
using System.Text;
namespace SIPO.Web.AjaxHandler.Loaddata
{
    public class getemisionvalue : SIPO.Web.BaseHandler
    {
        recTariffRepo recTarifDal = new recTariffRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recTransactionTypeRepo transTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
            recTransactionType transType = transTypeDal.GetByLikeTransTypeName("registrasi");
            decimal isFormula = 0;
            decimal? value = 0;
            decimal? minvalue = 0;
            decimal? maxvalue = 0;
            decimal? percentage = 0;
            decimal? result = 0;
            decimal? invoiceValue = 0;
            string transtype = string.Empty;
            int registrationEmisionTypeId = 0;
            string registrationlabel = string.Empty;
            decimal? nilaiEmisi = 0;
            int subSubSectorId = Convert.ToInt32(context.Request.Params["subSubSectorId"]);

            nilaiEmisi = (!string.IsNullOrEmpty(context.Request.Params["nilaiemisi"])) ? Helper.ToDecimal(context.Request.Params["nilaiemisi"].ToString(), System.Globalization.NumberStyles.Number, Helper.Culture.Indonesian) : 0m; // In All UI format must be Indonesian Format.
            
            if (context.Request.Params["transtype"] != null)
                transtype = context.Request.Params["transtype"].ToString();
            if (context.Request.Params["registrationEmisionTypeId"] != null)
                registrationEmisionTypeId = Convert.ToInt32(context.Request.Params["registrationEmisionTypeId"]);
            recTariff tarif = new recTariff();
            if (transtype == "perijinan" && context.Request.Params["registrationEmisionTypeId"] != null)
                tarif = recTarifDal.GetBySubSubSectorId(subSubSectorId, transType.TransTypeID, registrationEmisionTypeId);
            else
                tarif = recTarifDal.GetBySubSubSectorId(subSubSectorId, transType.TransTypeID);
            recSubSubSector subsubsector = recSubSubSectorDal.GetRegistrationLabelNameById(subSubSectorId);
            if (subsubsector != null)
                registrationlabel = subsubsector.recRegistrationLabel.RegistrationLabelName;
            isFormula = tarif.IsFormula == true ? 1 : 0;
            value = tarif.Value;
            minvalue = tarif.MinValue;
            maxvalue = tarif.MaxValue;
            percentage = tarif.Percentage;
            result = (nilaiEmisi * percentage) / 100;
            if (result > maxvalue)
                invoiceValue = maxvalue;
            else if (result < maxvalue && minvalue < result)
                invoiceValue = result;
            else if (minvalue > result)
                invoiceValue = minvalue;
            else if (result == maxvalue)
                invoiceValue = maxvalue;
            else if (result == minvalue)
                invoiceValue = minvalue;

            StringBuilder sbProject = new StringBuilder();
            sbProject.Append("{");
            sbProject.AppendFormat("  {0}IsFormula{0}:{0}{1}{0}", "\"", isFormula);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}Value{0}:{0}{1}{0}", "\"", value);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}MinValue{0}:{0}{1}{0}", "\"", minvalue);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}MaxValue{0}:{0}{1}{0}", "\"", maxvalue);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}Percentage{0}:{0}{1}{0}", "\"", percentage);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}InvoiceValue{0}:{0}{1}{0}", "\"", invoiceValue);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}RegistrationLabelName{0}:{0}{1}{0}", "\"", registrationlabel);
            sbProject.Append("}");

            context.Response.Write(sbProject.ToString());
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}